﻿Imports System.IO
Imports System.Windows.Forms

Public Class CharacterClass
    Private _id As Integer = -1
    Private _name As String = ""
    Private _desc As String = ""
    Private _minStr As Integer = 0
    Private _minDex As Integer = 0
    Private _minInt As Integer = 0
    Private _skillIDs As New IDCollection
    Private _itemIDs As New IDCollection

    Public Property ID() As Integer
        Get
            Return _id
        End Get
        Set(ByVal value As Integer)
            _id = value
        End Set
    End Property

    Public Property Name() As String
        Get
            Return _name
        End Get
        Set(ByVal value As String)
            _name = value
        End Set
    End Property

    Public Property Description() As String
        Get
            Return _desc
        End Get
        Set(ByVal value As String)
            _desc = value
        End Set
    End Property

    Public Property MinimumStrength() As Integer
        Get
            Return _minStr
        End Get
        Set(ByVal value As Integer)
            _minStr = value
        End Set
    End Property

    Public Property MinimumDexterity() As Integer
        Get
            Return _minDex
        End Get
        Set(ByVal value As Integer)
            _minDex = value
        End Set
    End Property

    Public Property MinimumIntelligence() As Integer
        Get
            Return _minInt
        End Get
        Set(ByVal value As Integer)
            _minInt = value
        End Set
    End Property

    Public Property Skills() As IDCollection
        Get
            Return _skillIDs
        End Get
        Set(ByVal value As IDCollection)
            _skillIDs = value
        End Set
    End Property

    Public Property Items() As IDCollection
        Get
            Return _itemIDs
        End Get
        Set(ByVal value As IDCollection)
            _itemIDs = value
        End Set
    End Property

    Public Function ToXElement() As XElement
        Dim classXML = <class id=<%= _id.ToString %>>
                           <name><%= _name %></name>
                           <desc><%= _desc %></desc>
                           <minStr><%= _minStr.ToString %></minStr>
                           <minDex><%= _minDex.ToString %></minDex>
                           <minInt><%= _minInt.ToString %></minInt>
                           <skills>
                               <%= From skillID In _skillIDs _
                                   Select <skillID><%= skillID.ToString %></skillID> %>
                           </skills>
                           <items>
                               <%= From itemID In _itemIDs _
                                   Select <itemID><%= itemID.ToString %></itemID> %>
                           </items>
                       </class>
        Return classXML
    End Function

    Public Sub LoadXML(ByVal classXML As String)
        Dim sr As New StringReader(classXML)
        Dim classX As XElement

        _skillIDs.Clear()
        _itemIDs.Clear()

        Try
            classX = XElement.Load(sr)
        Catch ex As Exception
            Failed(classXML, ex.ToString)
            Exit Sub
        End Try
        Try
            _id = Integer.Parse(classX.@id)
            _name = classX.<name>.Value
            _desc = classX.<desc>.Value
            _minStr = Integer.Parse(classX.<minStr>.Value)
            _minDex = Integer.Parse(classX.<minDex>.Value)
            _minInt = Integer.Parse(classX.<minInt>.Value)
            Dim skills = classX...<skills>...<skillID>
            For Each skill In skills
                _skillIDs.Add(Integer.Parse(skill.Value))
            Next
            Dim items = classX...<items>...<itemID>
            For Each item In items
                _itemIDs.Add(Integer.Parse(item.Value))
            Next
        Catch ex As Exception
            Failed(classXML, ex.ToString)
        End Try

    End Sub

    Private Sub Failed(ByVal xmlString As String, ByVal ex As String)
        MessageBox.Show(xmlString + vbCrLf + vbCrLf + ex, "Failed to LoadXML", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Sub

    Public ReadOnly Property IsValid() As Boolean
        Get
            Return CheckValidData()
        End Get
    End Property

    Private Function CheckValidData() As Boolean
        ' Check main details
        If _name = "" Or _desc = "" Then
            Return False
        End If

        If (_itemIDs.Count <= 0) Or (_skillIDs.Count <= 0) Then
            Return False
        End If
        Return True
    End Function
End Class
